package org.xserver.component.extension.filter.ssl;

import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xserver.component.core.XServerHttpContextAttachment;
import org.xserver.component.core.XServerHttpResponse;
import org.xserver.component.exception.FilterProcessError;
import org.xserver.component.exception.SslFilterError;
import org.xserver.component.extension.filter.AbstractContextFilter;

/**
 * @author bin
 * @since 2016-04-06
 */
public class XServerSslProcessFilter extends AbstractContextFilter {
	private static final Logger LOGGER = LoggerFactory.getLogger(XServerSslProcessFilter.class);

	@Override
	public void process(XServerHttpContextAttachment attachment) throws FilterProcessError {
		ChannelPipeline c = attachment.getChannelHandlerContext().getPipeline();
		if (c.get("ssl") == null) {
			LOGGER.error("此接口需要通过HTTPS进行访问{}", attachment.getRequest().getPath());
			XServerHttpResponse response = new XServerHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.METHOD_NOT_ALLOWED);
			response.setContentString("need ssl");
			attachment.getChannelHandlerContext().getChannel().write(response).addListener(ChannelFutureListener.CLOSE);
			//throw new SslFilterError(SslFilterError.SslFilterErrorEnum.NeedSsl);
		}
	}
}
